﻿@{
    ViewBag.Title = "角色权限";
    Layout = "~/Views/Shared/_Form.cshtml";
}

<script type="text/javascript">
    var roleId = request('roleId');
    var value = request('AuthorizeDataType');
    $(function () {
        initialPage();
        GetModuleTree();
        GetModuleButtonTree();   
    })
    //初始化页面
    function initialPage() {
        //加载导向
        $('#wizard').wizard().on('change', function (e, data) {
            var $finish = $("#btn_finish");
            var $next = $("#btn_next");
            if (data.direction == "next") {
                if (data.step == 1) {
                    var ModuleId = $("#ModuleTree").getCheckedAllNodes();
                    for (var i = 0; i < ModuleId.length; i++) {
                        var $thisid = $("#ModuleButtonTree").find('ul').find('[data-value=' + ModuleId[i] + ']').parent().parent();
                        $thisid.show();
                        $thisid.parents('ul').find('.' + ModuleId[i]).parent().show();
                    }
                }              
                if (data.step == 2) {
                    $finish.removeAttr('disabled');
                    $next.attr('disabled', 'disabled');
                }
            } else {
                $finish.attr('disabled', 'disabled');
                $next.removeAttr('disabled');
            }
        });
        //权限最末级
        $("#AuthorizeDateType").ComboBox({
            data: [{ "key": 0, "value": "机构" }, { "key": 1, "value": "小区" }, { "key": 2, "value": "楼栋" }, { "key": 4, "value": "楼层" }],
            id: "key",
            text: "value",
            description: "==选择权限层级==",
            click: function (e) {
                debugger;
                if (e.attr('data-value') == "") {
                    value = -1;
                    $("#OrganizePStructTree").attr('display', 'none');
                }
                else if (e.attr('data-value') != value) {
                    $("#OrganizePStructTree").attr('display', 'block');
                    value = parseInt(e.attr('data-value'));
                    GetOrganizePstructTree(value);
                }
            }
        });
        if (value != "") {
            // $("#OrganizePStructTree").attr('disabled', 'disabled');
            $("#AuthorizeDateType").ComboBoxSetValue(value);
            GetOrganizePstructTree(parseInt(value));
        }
        else {
            value = -1;
        }
        buttonOperation();
    }
    //获取系统功能
    function GetModuleTree() {
        var item = {
            height: 540,
            showcheck: true,
            url: "../../AuthorizeManage/PermissionRole/ModuleTreeJson?roleId=" + roleId
        };
        $("#ModuleTree").treeview(item);
    }
    //获取系统按钮
    function GetModuleButtonTree() {
        var item = {
            height: 540,
            showcheck: true,
            url: "../../AuthorizeManage/PermissionRole/ModuleButtonTreeJson?roleId=" + roleId
        };
        $("#ModuleButtonTree").treeview(item);
        $("#ModuleButtonTree").find('.bbit-tree-node-el').hide();
    }
   
    //获取机构楼盘数据架构
    function GetOrganizePstructTree(type) {
        $.ajax({
            url: "../../AuthorizeManage/PermissionRole/OrganizeTreeJson?roleId=" + roleId + "&AuthorizeDataType=" + type,
            type: "GET",
            dataType: "json",
            async: false,
            success: function (data) {
                var $treeJson = data.treeJson;
                var $authorizeType = data.authorizeType;
                var $authorizeData = data.authorizeData;
                var item = {
                    height: 495,
                    showcheck: true,
                    data: JSON.parse($treeJson),
                };
                $("#OrganizePStructTree").treeview(item);
                $("#OrganizePStructTree").find('li.bbit-tree-node').each(function () {
                    var $li = $(this);
                    $li.css({ position: 'relative' });
                    debugger;
                    var _data_value = $li.find('a').find('span').attr('data-value');
                    var _data_title = $('#OrganizePStructTree_' + _data_value.replace(/-/g, "_")).attr('title');
                    var _html = '<div style="position: absolute;display:none;right: 0px;top:4px;z-index: 1;"><div class="checkbox">';
                    _html += '<label><input name="' + _data_value + '" type="checkbox" value="' + _data_title + '" /></label>';
                    _html += '</div></div>';
                    $li.append(_html);
                });
            }
        });
    }

    //按钮操作（上一步、下一步、完成、关闭）
    function buttonOperation() {
        var $last = $("#btn_last");
        var $next = $("#btn_next");
        var $finish = $("#btn_finish");
        //完成提交保存
        $finish.click(function () {
            var postData = $("#form1").GetWebControls();
            postData["roleId"] = roleId;
            postData["moduleIds"] = String($("#ModuleTree").getCheckedAllNodes());
            postData["moduleButtonIds"] = String($("#ModuleButtonTree").getCheckedAllNodes());
            //postData["moduleColumnIds"] = String($("#ModuleColumnTree").getCheckedAllNodes());
            postData["AuthorizeDataType"] = value;
            postData["authorizeDataJson"] = JSON.stringify(GetDataAuthorize());
            $.SaveForm({
                url: "../../AuthorizeManage/PermissionRole/SaveAuthorize",
                param: postData,
                loading: "正在保存角色授权...",
                success: function () {
                    $.currentIframe().$("#gridTable").trigger("reloadGrid");
                }
            })
        })
    }
    //获取数据范围权限选中值、返回Json
    function GetDataAuthorize() {
        var dataAuthorize = [];           
        var selectedData = $("#OrganizePStructTree").getCheckedAllNodes();
            for (var i = 0; i < selectedData.length; i++) {
                var ResourceId = selectedData[i];
                var ItemType = $("input[name='" + ResourceId + "']").val();
                var rowdata = {
                    ResourceId: ResourceId,
                    ItemType: ItemType,
                    AuthorizeType: 2
                }
                dataAuthorize.push(rowdata);
            }        
        return dataAuthorize;
    }
</script>
<div class="widget-body">
    <div id="wizard" class="wizard" data-target="#wizard-steps" style="border-left: none; border-top: none; border-right: none;">
        <ul class="steps">
            <li data-target="#step-1" class="active"><span class="step">1</span>功能权限<span class="chevron"></span></li>
            <li data-target="#step-2"><span class="step">2</span>操作权限<span class="chevron"></span></li>
            <li data-target="#step-3"><span class="step">3</span>数据权限<span class="chevron"></span></li>        
        </ul>
    </div>
    <div class="step-content" id="wizard-steps" style="border-left: none; border-bottom: none; border-right: none;">
        <div class="step-pane active" id="step-1">
            <div id="ModuleTree" style="margin: 10px;"></div>
        </div>
        <div class="step-pane" id="step-2">
            <div id="ModuleButtonTree" style="margin: 10px;"></div>
        </div>       
        <div class="step-pane" id="step-3" style="margin: 15px; margin-bottom: 0px;">
            <div>
                <ul class="list-group">
                    <li class="list-group-item">
                        <div>
                            <label style="color:darkred">
                                数据权限验证层级:
                            </label>
                            <div id="AuthorizeDateType" type="selectTree " class="ui-select"></div>
                        </div>
                        <div id="OrganizePStructTree" style="margin: 10px;"></div>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>
<div class="form-button" id="wizard-actions">
    <a id="btn_last" disabled class="btn btn-default btn-prev">上一步</a>
    <a id="btn_next" class="btn btn-default btn-next">下一步</a>
    <a id="btn_finish" disabled class="btn btn-success">完成</a>
</div>
